package fun.sboy.panel;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import fun.sboy.api.API;
import fun.sboy.listener.MouseCLickListener;
import fun.sboy.pojo.ManagerUser;
import fun.sboy.util.JDBCUtil;
import fun.sboy.view.HomeView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalTime;
import java.util.ArrayList;

public class SubAccountPanel extends BaseJPanel {

    private JTextField remitter = new JTextField();
    private JTextField remitterCard = new JTextField();
    private JTextField payee = new JTextField();
    private JTextField payeeCard = new JTextField();
    private JTextField amount = new JTextField();

    private JButton submit = new JButton("汇款");

    public SubAccountPanel(LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
        initialization();
    }

    public SubAccountPanel(LayoutManager layout) {
        super(layout);
        initialization();
    }

    public SubAccountPanel(boolean isDoubleBuffered) {
        super(isDoubleBuffered);
        initialization();
    }

    public SubAccountPanel() {
        initialization();
    }

    @Override
    public void initialization() {
        ManagerUser user = HomeView.user;

        String[] title = {"汇款人  ：", "汇款账号：", "收款人  ：", "收款账号：", "汇款金额："};
        int x = 60, y = 40;
        for (String t : title) {
            JLabel j = new JLabel(t);
            j.setBounds(x, y, 200, 30);
            y = x >= 430 ? y + 60 : y;
            x = x >= 430 ? 60 : 430;
            add(j);
        }

        remitter.setText(user.getName());
        remitter.setEnabled(false);
        remitterCard.setText(user.getCardNumber());
        remitterCard.setEnabled(false);
        ArrayList<JTextField> list = new ArrayList<>();
        list.add(remitter);
        list.add(remitterCard);
        list.add(payee);
        list.add(payeeCard);
        addAmountKeyListener();
        list.add(amount);

        x = 130;
        y = 40;
        Insets insets = new Insets(0, 10, 0, 10);
        for (int i = 0; i < list.size(); i++) {
            JTextField j = list.get(i);
            j.setMargin(insets);
            j.setBounds(x, y, 200, 30);
            add(j);
            if (i % 2 == 0) {
                x += 450;
            } else {
                y += 60;
                x = 130;
            }
        }

        submit.setBounds(280, 240, 200, 30);
        submit.setBorder(BorderFactory.createEmptyBorder());
        submit.setBackground(Color.gray);
        submit.setForeground(Color.white);

        /**
         * 添加点击事件
         */
        ManagerUser finalUser = user;
        submit.addMouseListener(new MouseCLickListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String payeeName = payee.getText();
                String payeeCardNumber = payeeCard.getText();
                String money = amount.getText();

                //对收款人以及收款账号进行不为空验证
                if (payeeName.equals("") || payeeCardNumber.equals("")) {
                    JOptionPane.showMessageDialog(null, "收款人和收款卡号不能为空，请检查！");
                    return;
                }

                //不能给自己汇款
                if (payeeName.equals(finalUser.getName()) || payeeCardNumber.equals(finalUser.getCardNumber())) {
                    JOptionPane.showMessageDialog(null, "不能给自己进行汇款！");
                    return;
                }

                //收款卡号持有人与卡号验证
                ManagerUser payeeUser = API.isNameAndCardNumber(payeeName, payeeCardNumber);
                if (payeeUser == null) {
                    JOptionPane.showMessageDialog(null, "收款人与收款卡号不匹配，请检查后重试提交！");
                    return;
                }

                //验证金额
                if (money.equals("") || money.equals("0")) {
                    JOptionPane.showMessageDialog(null, "汇款金额不能为空不能为0");
                }
                Double moneyTextFieldVal = Double.parseDouble(money);
                Double moneyBalance = API.getMoneyBalance();
                if (moneyBalance == null) {
                    JOptionPane.showMessageDialog(null, "数据库错误，请联系开发者！");
                    return;
                }
                if (moneyBalance < moneyTextFieldVal) {
                    JOptionPane.showMessageDialog(null, "汇款金额不能大于当前剩余金额！");
                    return;
                }


                //提交交易订单
                try {

                    //转账
                    API.submitOrder(moneyBalance, moneyTextFieldVal, payeeUser);
                    JOptionPane.showMessageDialog(null, "转账成功！！！", "转账成功", JOptionPane.INFORMATION_MESSAGE);

                } catch (SQLException ex) {
                    ex.printStackTrace();
                    //失败反馈
                    JOptionPane.showMessageDialog(null, "数据库操作失败，请联系管理员！", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        add(submit);
    }

    /**
     * 添加案件监听事件
     */
    private void addAmountKeyListener() {
        amount.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (c != '.' && (c < '0' || c > '9')){
                    if (c != '\n'){
                        e.setKeyChar('\0');
                    }
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {

            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });
    }

    @Override
    public void reload() {

    }
}
